﻿using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using DataBaseConnect.App;
using Entity;
using Entity.Dto;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Utils;

namespace CoreMVC.Jurisdiction.Manage.Controllers
{
    public class LeaveController : BaseController
    {
        #region 构造实例化

        private readonly ILeaveAppService _leaveAppService;
        private readonly IFlowAppService _flowAppService;

        public LeaveController(ILeaveAppService leaveAppService
            , IFlowAppService flowAppService)
        {
            _leaveAppService = leaveAppService;
            _flowAppService = flowAppService;
        }

        #endregion

        #region 视图区

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加页
        /// </summary>
        /// <returns></returns>
        public IActionResult AddLeave()
        {
            var flow = _flowAppService.GetFlowList("");
            return View(flow);
        }
        #endregion

        #region 功能实现区

        public JsonResult GetLeaveAllMsg(string Name)
        {
            ReceiveStatus<Leave> receiveStatus = new ReceiveStatus<Leave>();

            List<Leave> list = new List<Leave>();
            try
            {
                //得到所有流程
                list = _leaveAppService.GetLeaveAllMsg(Name);
                receiveStatus.msg = "成功";
                receiveStatus.data = list;
            }
            catch (Exception ex)
            {
                receiveStatus.msg = "失败：" + ex.Message;
                receiveStatus.code = statuCode.Error;
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 新增假单
        /// </summary>
        /// <param name="Str">参数字符串</param>
        /// <returns></returns>
        public JsonResult AddLeaveMsg(List<string> Str, string FlowContent, string FormContent)
        {
            string sb = Str[0].Replace("{", "");
            string sb2 = sb.Replace("}", "");
            string[] array = sb2.Split(',');
            string[] temp = null;
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            foreach (string s in array)
            {
                temp = s.Split(':');
                dictionary.Add(temp[0].Trim('"'), temp[1]);
            }
            int fields = Convert.ToInt32(dictionary["Fields"].Trim('"'));
            string html = FormContent;
            for (int i = 1; i <= fields; i++)
            {
                string value = dictionary["data_" + i + ""].Trim('"');
                html = html.Replace("{data_" + i + "}", value);
            }
           

           // ApprovalStatuDto approvalStatuDto = JsonConvert.DeserializeObject<ApprovalStatuDto>(Str[0]);
            Leave leave = JsonConvert.DeserializeObject<Leave>(Str[0]);
            return null;
            leave.FormContent = html;
            leave.FlowContent = FlowContent;
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                leave.CreateTime = DateTime.Now;
                leave.CreateUser = Convert.ToInt32(User.FindFirst(ClaimTypes.Sid).Value);
                leave.StarTime = DateTime.Now;
                leave.EndTime = DateTime.Now.AddDays(1);
                var result = _leaveAppService.AddLeaveMsg(leave);
                receiveStatus.msg = result.msg;
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "新增失败：" + ex.Message;
            }
            return Json(receiveStatus);
        }
        #endregion
    }
}